package com.tedu.servies;

import com.tedu.mapper.SmbmsUserMapper;
import com.tedu.pojo.SmbmsUser;
import com.tedu.pojo.SmbmsUserExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 用户服务层接口实现类    交给spring管理
 */
@Service  //组件是服务层实现类，交给spring管理
public class UserServiesImpl implements UserServies {
    // 完成了spring整合mybatis
    //spring容器中，已经有mapper实现类对象
    //从spring容器中，取mapper对象
    @Autowired
    SmbmsUserMapper  userMapper;

    // 登陆
    @Override
    public SmbmsUser getUserByLogin(SmbmsUser u) {

        //使用mybatis获取数据库的数据
        //
        SmbmsUserExample example = new SmbmsUserExample();
        SmbmsUserExample.Criteria criteria = example.createCriteria();
        criteria.andUsercodeEqualTo(u.getUsercode());
        criteria.andUserpasswordEqualTo(u.getUserpassword());
        List<SmbmsUser> smbmsUsers = userMapper.selectByExample(example);
        if(smbmsUsers.size()>0){
            return smbmsUsers.get(0);
        }

        return null;
    }

    @Override
    public List<SmbmsUser> getAllUser() {
        return userMapper.selectByExample(new SmbmsUserExample());
    }

    @Override
    public SmbmsUser getUserById(int uid) {
        return userMapper.selectByPrimaryKey(new Long(uid));
    }

    @Override
    public boolean getUserByCode(String userCode) {
        //使用mybatis获取数据库的数据
        //
        SmbmsUserExample example = new SmbmsUserExample();
        SmbmsUserExample.Criteria criteria = example.createCriteria();
        criteria.andUsercodeEqualTo(userCode);
        List<SmbmsUser> smbmsUsers = userMapper.selectByExample(example);
        if(smbmsUsers.size()>0){
            return true;
        }

        return false;
    }
}
